package com.systop.tea.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.systop.tea.common.R;
import com.systop.tea.dto.CartDto;
import com.systop.tea.entity.Cart;
import com.systop.tea.entity.Product;
import com.systop.tea.service.CartService;
import com.systop.tea.service.ProductService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequiredArgsConstructor
@RequestMapping("/cart")
public class CartController {

    private final CartService cartService;
    private final ProductService productService;

    @GetMapping("/query")
    public R<List<CartDto>> query(Cart cart) {
        LambdaQueryWrapper<Cart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(cart.getUserId() != null, Cart::getUserId, cart.getUserId());
        List<Cart> carts = cartService.list(queryWrapper);

        List<CartDto> cartDtos= carts.stream().map(item -> {
            CartDto cartDto = new CartDto();
            BeanUtils.copyProperties(item, cartDto);
            Product product = productService.getById(item.getProductId());
            if (product != null) {
                cartDto.setProductName(product.getProductName());
                cartDto.setThumbnail(product.getThumbnail());
                cartDto.setReprice(product.getReprice());
            }
            return cartDto;
        }).collect(Collectors.toList());

        return R.success(cartDtos);
    }

    @PostMapping
    public R<String> add(@RequestBody Cart cart) {
        boolean row = cartService.save(cart);
        if (row) {
            return R.success("加购成功");
        } else {
            return R.error("加购失败");
        }
    }

    @PutMapping
    public R<String> modify(@RequestBody Cart cart) {
        boolean row;
        if (cart.getUserId() != null) {
            LambdaUpdateWrapper<Cart> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(Cart::getUserId, cart.getUserId());
            updateWrapper.set(Cart::getDone, cart.getDone());
            row = cartService.update(updateWrapper);
        } else {
            row = cartService.updateById(cart);
        }
        if (row) {
            return R.success("购物车修改成功");
        } else {
            return R.error("购物车修改失败");
        }
    }

    @DeleteMapping("/{id}")
    public R<String> delete(@PathVariable Integer id) {
        boolean row = cartService.removeById(id);
        if (row) {
            return R.success("购物车删除成功");
        } else {
            return R.error("购物车删除失败");
        }
    }

    @DeleteMapping("/dels")
    public R<String> deletes(@RequestBody Cart cart) {
        LambdaQueryWrapper<Cart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Cart::getUserId ,cart.getUserId());
        queryWrapper.eq(Cart::getDone, cart.getDone());
        boolean row = cartService.remove(queryWrapper);

        if (row) {
            return R.success("购物车删除成功");
        } else {
            return R.error("购物车删除失败");
        }
    }

}
